package com.sakila.test.aop;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class AopClass implements MethodBeforeAdvice, AfterReturningAdvice{

	long startTime = 0;

	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println("before this method: selectAll");
		startTime = System.nanoTime();
	}
	
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args,
			Object target) throws Throwable {	
		long elapsedTime = System.nanoTime() - startTime;
		String className = target.getClass().getCanonicalName();
		String methodName = method.getName();
		System.out.println("AOP: Execution of " + className + "#" 
				+ methodName + " ended in " + new BigDecimal(elapsedTime).
				divide(new BigDecimal(1_000_000)) + " milliseconds");		
	}

}
